package cn.cms.service;

import cn.cms.dao.SysRoleDao;
import cn.cms.model.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SysRoleService {


    @Autowired
    private SysRoleDao sysRoleDao;

    public String getCheckedRoleIds(List<SysRole> sysRoles) {
        Set<String> roleIds = new HashSet<>();
        getCheckedRoleIds(sysRoles, roleIds);
        StringBuilder sb = new StringBuilder();
        roleIds.forEach(k -> {
            sb.append(k).append(",");
        });
        if (sb.length() > 0) {
            return sb.substring(0, sb.length() - 1);
        }
        return "";
    }

    private void getCheckedRoleIds(List<SysRole> sysRoles, Set<String> roleIds) {
        sysRoles.forEach(k -> {
            Map<String, Boolean> temp = k.getState();
            if (temp != null && temp.get("checked") != null && k.getState().get("checked") == true) {
                roleIds.add(String.valueOf(k.getId()));
            }
            if (k.getNodes() != null && k.getNodes().size() > 0) {
                getCheckedRoleIds(k.getNodes(), roleIds);
            }
        });
    }

    public List<SysRole> getRoles(int maxLevel) {
        List<SysRole> tops = sysRoleDao.getTop();
        tops.forEach(k -> {
            if (k.getLevel() <= maxLevel) {
                k.setSelectable(false);
            } else {
                k.setSelectable(true);
            }
            // 根角色拥有自修改权限
            if (maxLevel == 0) {
                k.setSelectable(true);
            }
            k.setText(k.getRoleName());
            getByPid(k, maxLevel);
        });
        return tops;
    }

    public List<SysRole> getRoles() {
        List<SysRole> tops = sysRoleDao.getTop();
        tops.forEach(k -> {
            k.setText(k.getRoleName());
            getByPid(k);
        });
        return tops;
    }

    public List<SysRole> getRoles(List<String> roleIds) {
        List<SysRole> tops = sysRoleDao.getTop();
        tops.forEach(k -> {
            k.setText(k.getRoleName());
            if (roleIds.contains(String.valueOf(k.getId()))) {
                Map<String, Boolean> temp = new HashMap<>();
                temp.put("checked", true);
                k.setState(temp);
            }
            getByPid(k, roleIds);
        });
        return tops;
    }

    public int getMaxLevel(List<Integer> ids) {
        return sysRoleDao.getMaxLevel(ids);
    }

    public int getMaxSort(int pid) {
        return sysRoleDao.getMaxSort(pid);
    }

    public int getLevelById(int id) {
        return sysRoleDao.getLevelById(id);
    }

    public SysRole getRoleById(int id) {
        return sysRoleDao.get(id);
    }

    public void delete(SysRole sysRole) {
        sysRoleDao.delete(sysRole);
    }

    public void update(SysRole sysRole) {
        sysRoleDao.update(sysRole);
    }

    public void save(SysRole sysRole) {
        sysRoleDao.save(sysRole);
    }

    /**
     * 逐级获取菜单节点
     *
     * @param sysRole
     */
    private void getByPid(SysRole sysRole, int maxLevel) {
        List<SysRole> sysRoles = sysRoleDao.getByPid(sysRole.getId());
        if (sysRoles != null && sysRoles.size() > 0) {
            sysRoles.forEach(k -> {
                if (k.getLevel() <= maxLevel) {
                    k.setSelectable(false);
                } else {
                    k.setSelectable(true);
                }
                k.setText(k.getRoleName());
                getByPid(k, maxLevel);
            });
            sysRole.setNodes(sysRoles);
        }
    }

    /**
     * 逐级获取菜单节点
     *
     * @param sysRole
     */
    private void getByPid(SysRole sysRole) {
        List<SysRole> sysRoles = sysRoleDao.getByPid(sysRole.getId());
        if (sysRoles != null && sysRoles.size() > 0) {
            sysRoles.forEach(k -> {
                k.setText(k.getRoleName());
                getByPid(k);
            });
            sysRole.setNodes(sysRoles);
        }
    }

    /**
     * 逐级获取菜单节点
     *
     * @param sysRole
     */
    private void getByPid(SysRole sysRole, List<String> roleIds) {
        List<SysRole> sysRoles = sysRoleDao.getByPid(sysRole.getId());
        if (sysRoles != null && sysRoles.size() > 0) {
            sysRoles.forEach(k -> {
                k.setText(k.getRoleName());
                if (roleIds.contains(String.valueOf(k.getId()))) {
                    Map<String, Boolean> temp = new HashMap<>();
                    temp.put("checked", true);
                    k.setState(temp);
                }
                getByPid(k, roleIds);
            });
            sysRole.setNodes(sysRoles);
        }
    }


}
